package spring.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by Administrator on 2014/6/13.
 */
public class UserInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        try{

            if (methodInvocation.getMethod().getName().equals("printUser"))
            // 拦截方法是否是UserService接口的printUser方法
            {
                Object[] args = methodInvocation.getArguments();// 被拦截的参数
                System.out.println("user:" + args[0]);
                methodInvocation.getArguments()[0] = "hello!";// 修改被拦截的参数

            }

            System.out.println(methodInvocation.getMethod().getName() + "---!");
            return methodInvocation.proceed();// 运行UserService接口的printUser方法

        } catch (Exception e){
            throw e;
        }
    }
}
